<!DOCTYPE html>
<html lang="en"  xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>产品管理</title>
    <link rel="stylesheet" href="/webjars/layui/2.5.6/css/layui.css">
    <script src="/webjars/layui/2.5.6/layui.js" type="text/javascript"></script>
</head>
<body>

<script id="userTool" type="text/html">
    <button class="layui-btn" onclick="userAdd()">添加</button>
    <button class="layui-btn" onclick="batchDel()">批量删除</button>
</script>
<script id="userDCL" type="text/html">
    <button class="layui-btn" lay-event="userUpdate">修改</button>
</script>

<div>

    <div class="layui-collapse">
        <div class="layui-colla-item">
            <h2 class="layui-colla-title">条件</h2>
            <div class="layui-colla-content layui-show">
                <form class="layui-form layui-input-inline">
                    <div class="layui-form-item layui-input-inline">
                        <label class="layui-form-label">状态</label>
                        <div class="layui-input-inline">
                            <select name="status" id="status" lay-verify="required">
                                <option value="">全部</option>
                            </select>
                        </div>
                    </div>
                    <button type="button" class="layui-btn" onclick="searchByUserName()">查询</button>
                </form>
            </div>
        </div>

    </div>

    <table id="userTable" lay-filter="userTable" class="layui-table"></table>
</div>

<div id="userHandler" style="display: none">
    <form class="layui-form" id="userForm" lay-filter="userForm" style="padding: 35px;">
        <div class="layui-form-item">
            <label class="layui-form-label">工艺要求编号</label>
            <div class="layui-input-inline">
                <input type="text" name="technologyRequirementId" required  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工艺名称</label>
            <div class="layui-input-inline">
                <select name="technologyId" id="technologyId"  lay-verify="required">
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工艺要求</label>
            <div class="layui-input-inline">
                <input type="text" name="requirement" required  lay-verify="required" placeholder="请输入" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">工艺要求添加时间</label>
            <div class="layui-input-inline">
                <input id="addTime" lat-filter="addTime"  name="addTime" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">工艺要求修改时间</label>
            <div class="layui-input-inline">
                <input id="reviseTime" lat-filter="reviseTime"  name="reviseTime" required  lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input">
            </div>
        </div>


    </form>
</div>
</body>
<script>
    function userAdd() {
        userDCL("addTechnologyRequire");
    }
    function userUpdate(data) {
        userDCL("updateTechnologyRequire",data);
        // console.log(data);
    }

    function userDCL(path,data) {
        layui.use(['table', 'form', 'jquery', 'layer'], function () {
            var table = layui.table;
            var form = layui.form;
            var $ = layui.jquery;
            var layer = layui.layer;
            var msg="修改"
            if(path=="addTechnologyRequire"){
                $("#userForm")[0].reset();
                msg="添加";
            }else {
                form.val("userForm",data);
            }
            layer.open({
                type: 1,
                title: msg,
                area: ["500px","450px"],
                content: $("#userHandler"),
                btn: ['确定', '取消'],
                yes() {
                    let user = form.val("userForm");
                    console.log(user)
                    $.post(path,user).done(({msg})=>{
                        layer.close(layer.index)
                        layer.msg(msg);
                        form.render('select');
                        table.reload("userTable")
                    })
                }
            })
        })
    }
    function searchByUserName(){
        var $=layui.jquery;
        var status=$("#status").val();
        layui.table.reload('userTable',{where:{status:status}})
    }
    layui.use(['table','form','laydate','jquery','element','layedit'],function () {
        //创建一个编辑器

        let table=layui.table;
        let form=layui.form;
        let $=layui.jquery;
        let laydate=layui.laydate;
        let layedit=layui.layedit;
        table.render({
            id:"userTable",
            elem:"#userTable",
            url:"/technologyRequirement/list",
            toolbar:"#userTool",
            cols:[
                [
                    {type:"checkbox"},
                    {title:"工艺要求编号",field:"technologyRequirementId"},
                    {title:"工艺名称  ",templet(d){return d.technology.technologyName;}},
                    {title:"工艺要求 ",field:"requirement"},
                    {title:"工艺要求添加时间",field:"addTime"},
                    {title:"工艺要求修改时间",field:"reviseTime"},
                    {title:"操作",toolbar: "#userDCL"}
                ]
            ],
            page:{
                limit:6,
                limits:[9,12,15]
            }
        });
        table.on("tool(userTable)",function ({data,event}) {
            switch (event) {
                case "userUpdate":
                    userUpdate(data);
            }
        })
        laydate.render({
            elem: '#addTime'
            ,format: 'yyyy-MM-dd HH:mm' //可任意组合
        });
        laydate.render({
            elem: '#reviseTime'
            ,format: 'yyyy-MM-dd HH:mm' //可任意组合
        });

        $.get("/manufacture/technologyList",({data})=>{
            for (let d of data) {
                console.log(d)
                var option=$("<option></option>");
                option.html(d.technologyName);
                option.val(d.technologyId);
                $("#technologyId,#status").append(option);
            }
            form.render('select');
        })


    })
    //批量删除
    function batchDel() {
        layui.use(['table','layer','jquery'],function () {
            var table=layui.table;
            var layer=layui.layer;
            var $=layui.jquery;
            var product=table.checkStatus("userTable").data;
            var ids=product.map(product=>"ids="+product.technologyRequirementId).join("&")
            if(ids==""){
                layer.msg("请选择要删除的产品")
                return;
            }
            console.log(product)
            console.log(ids)
            $.get("/technologyPlan/patchDel?"+ids,({msg})=>{
                layer.msg(msg)
                table.reload("userTable");
            })

        })
    }
</script>
</body>
</html>